//
//  RechargeViewController.m
//  AipuTel
//
//  Created by cretve on 15/1/15.
//  Copyright (c) 2015年 aipu. All rights reserved.
//

#import "RechargeViewController.h"

@interface RechargeViewController ()

@property (weak, nonatomic) IBOutlet UITextField *phoneField;
@property (weak, nonatomic) IBOutlet UITextField *cardNumField;
@property (weak, nonatomic) IBOutlet UITextField *passwordField;

@end

@implementation RechargeViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.title = @"充值";
    
    _passwordField.bk_shouldReturnBlock = ^BOOL(UITextField* textField){
        [textField resignFirstResponder];
        return YES;
    };
}

- (IBAction)clickedRecharge:(id)sender
{
    if(_phoneField.text.length != 11)
    {
        [UIAlertView bk_showAlertViewWithTitle:@"请输入正确的手机号" message:nil cancelButtonTitle:@"好" otherButtonTitles:nil handler:nil];
        return;
    }
    
    if(_cardNumField.text.length == 0)
    {
        [UIAlertView bk_showAlertViewWithTitle:@"请输入充值卡卡号" message:nil cancelButtonTitle:@"好" otherButtonTitles:nil handler:nil];
        return;
    }
    
    if(_passwordField.text.length == 0)
    {
        [UIAlertView bk_showAlertViewWithTitle:@"请输入充值卡密码" message:nil cancelButtonTitle:@"好" otherButtonTitles:nil handler:nil];
        return;
    }
    
    [_phoneField resignFirstResponder];
    [_passwordField resignFirstResponder];
    [_cardNumField resignFirstResponder];
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.labelText = @"请稍候...";
    
    __weak RechargeViewController* weakSelf = self;
    [[AFHTTPRequestOperationManager manager] POST:@"http://202.98.133.196/ddserver/pay" parameters:@{@"username": _phoneField.text, @"pin": _cardNumField.text, @"password": _passwordField.text, @"type": @"2"} success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [MBProgressHUD hideAllHUDsForView:weakSelf.view animated:YES];
        
        if([responseObject[@"errno"] integerValue] == 0)
        {
            [UIAlertView bk_showAlertViewWithTitle:@"充值成功" message:nil cancelButtonTitle:@"好" otherButtonTitles:nil handler:nil];
        }
        else
        {
            [UIAlertView bk_showAlertViewWithTitle:responseObject[@"info"] message:nil cancelButtonTitle:@"好" otherButtonTitles:nil handler:nil];
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [MBProgressHUD hideAllHUDsForView:weakSelf.view animated:YES];
        [UIAlertView bk_showAlertViewWithTitle:NetworkErrorMessage message:nil cancelButtonTitle:@"好" otherButtonTitles:nil handler:nil];
    }];
}

@end
